package com.aix.demo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

import com.aix.demo.R;

public class CalculatorActivity extends Activity {
    String[] chars = new String[] { "7", "8", "9", "+", "4", "5", "6", "-",
            "1", "2", "3", "*", ".", "0", "=", "/" };
    GridLayout layout;
    TextView tv;
    String first = "", second = "", sign = "";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        layout = (GridLayout) findViewById(R.id.root);
        tv = (TextView) findViewById(R.id.result);
        for (int i = 0; i < chars.length; i++) {
            final Button bn = new Button(this);
            bn.setText(chars[i]);
            bn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    CharSequence vl = bn.getText();
                    if ("+-*/".indexOf(vl.toString()) >= 0) {
                        if (!"".equals(sign))
                            calculate(first, second, sign);
                        if (!"".equals(first))
                            sign = vl.toString();
 
                    } else if ("=".equals(vl.toString())) {
                        calculate(first, second, sign);
                    } else {
                        if ("".equals(sign))
                            first = addValue(first, vl);
                        else
                            second = addValue(second, vl);
                    }
 
                    if (!"".equals(first))
                        tv.setText(first + sign + second);
                }
            });
             
            GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 2);
            GridLayout.Spec columnSpec = GridLayout.spec(i % 4);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(
                    rowSpec, columnSpec);
            params.setGravity(Gravity.FILL | Gravity.BOTTOM);
            layout.addView(bn, params);
        }
    }
 
    public void cleanHandler(View source) {
        first = "";
        second = "";
        sign = "";
        tv.setText("");
    }
 
    private String addValue(String value, CharSequence vl) {
        if ("".equals(value) && ".".equals(vl))
            value += "0.";
        else if (".".equals(vl) && value.indexOf(".") > 0) {
 
        } else if ("".equals(value) && "0".equals(vl)) {
 
        } else
            value += vl;
        return value;
    }
 
    private void calculate(String first, String second, String sign) {
        if ("".equals(first) || "".equals(second))
            return;
        Double value1 = Double.parseDouble(first);
        Double value2 = Double.parseDouble(second);
        Double result = 0d;
        if ("+".equals(sign))
            result = value1 + value2;
        else if ("-".equals(sign))
            result = value1 - value2;
        else if ("*".equals(sign))
            result = value1 * value2;
        else if ("/".equals(sign))
            result = value1 / value2;
        this.sign = "";
        this.second = "";
        this.first = String.valueOf(result);
    }
}